<?php

class IcategoryController extends MyController
{
	function init()
	{
		parent::init();
	}
	
	function actionIndex()
	{	
		$this->redirect(array($this->controller_id.'/view'));
	}	
	
	function actionView($parent_id = 0, $page = 1)
	{
		$pages = new CPagination;
		$pages->pageSize = $this->website->beperpage;
		
		$rows = NULL;
		
		if ($parent_id == 0)
		{
			$rows_temp = array();
			
			$parent = icategory::model()->findAll(array(
				"condition" => "parent_id = 0",
				"order" => "`order`"
			));
			
			foreach($parent as $p)
			{
				$rows_temp[] = $p;
				$sub = $p->sub_category(array("order"=>"`order`"));
				
				foreach($sub as $s)
				{
					$rows_temp[] = $s;
				}
			}
			$pages->itemCount = count($rows_temp);
			
			$offset = ($page - 1) * $this->website->beperpage;
			
			$delta = 0;
			if ($page == 1)
				$delta = min($pages->itemCount, $this->website->beperpage);
			else if ($page == $pages->pageCount)
			{
				if ($pages->itemCount % $this->website->beperpage == 0)
					$delta = $this->website->beperpage;
				else
					$delta = min($pages->itemCount % $this->website->beperpage, $this->website->beperpage);
			}
			else
				$delta = $this->website->beperpage;
						
			$end = $offset + $delta;
			for($i = $offset; $i < $end ; $i++)
			{
				$rows[] = $rows_temp[$i];
			}
			
		}else{
			$rows = icategory::model()->findAll(array(
				"condition"=>"parent_id = " . $parent_id,
				"order" => "`order` ASC",
				"offset" => ($page - 1) * $this->website->beperpage,
				"limit" => $this->website->beperpage
			));
			
			$pages->itemCount = icategory::model()->count(array(
				"condition"=>"parent_id = " . $parent_id,
				"order" => "`order`ASC"
			));
		}
		$this->data['rows']= $rows;
		$this->data['parent_id']= $parent_id;
		$this->data['pages'] = $pages;
		$this->data['page']= $page;
		$this->render('view', $this->data);
	}
	
	function actionAdd($parent_id = 0, $page = 1)
	{
		$this->action = "Thêm mới";
		$row = new icategory;
		$row->id = 0;
		$row->order = Yii::app()->db->createCommand(
			"SELECT max(`order`) FROM icategory WHERE parent_id = " . $parent_id
		)->queryScalar() + 1;
		$this->data["row"] = $row;
		$this->data['parent_id']= $parent_id;
		$this->data['page']= $page;	
		$this->render("form_edit",$this->data);
	}
	
	function actionEdit($icategory_id = 0, $parent_id = 0, $page = 1)
	{
		$this->action = "Cập nhật";
		$row = NULL;
		
		if ($icategory_id == 0)
		{
			$this->redirect(array($this->controller_id.'/view',
				"parent_id"=>$parent_id, "page" => $page));
		}else{
			$row = icategory::model()->findByPk($icategory_id);
			$this->data['row'] = $row;
			$this->data['parent_id'] = $parent_id;	
			$this->data["page"] = $page;
			$this->render("form_edit", $this->data);
		}
	}
	
	function actionSave($icategory_id = 0, $parent_id = 0, $page = 1)
	{
		$row = NULL;
		if ($icategory_id == 0)
		{
			$row = new icategory;
			$row->setAttributes($_POST, false);
			$row->save();
		}else{
			$row = icategory::model()->findByPk($icategory_id);
			$row->setAttributes($_POST, false);
			if ($row->id != Yii::app()->getRequest()->getPost("parent_id"))
				$row->save();
		}
		$this->redirect(array($this->controller_id.'/view',
			"parent_id"=>$parent_id, "page" => $page));
	}
	
	function actionEnable($icategory_id, $parent_id = 0, $page = 1)
	{
		$row = icategory::model()->findByPk($icategory_id);
		$row->enable = 1 - $row->enable;
		$row->save();
		$this->redirect(array($this->controller_id.'/view',
			"parent_id"=>$parent_id, "page" => $page));
	}
	
	function actionSet_order($parent_id = 0, $page = 1)
	{
		//Tao thu tu mac dinh
		$rows = icategory::model()->findAll();
		foreach($rows as $row)
		{
			$row->order = $row->id;
			$row->save();
		}
		$this->redirect(array($this->controller_id.'/view', 
			"parent_id"=>$parent_id, "page"=>$page));	
	}
	
	function actionSave_order($parent_id = 0, $page = 0)
	{
		$orders = Yii::app()->getRequest()->getPost("order");
		
		foreach($orders as $id=>$value)
		{
			$row = icategory::model()->findByPk($id);
			$row->order = $value;
			$row->save();
		}
		
		//print_r($orders);
		$this->redirect(array($this->controller_id.'/view', 
			"parent_id"=>$parent_id, "page"=>$page));		
	}
	
	function actionDelete($icategory_id, $parent_id = 0, $page = 1)
	{
		$row = icategory::model()->findByPk($icategory_id);
		$count_sub = count($row->sub_category);
		$count_image = count($row->images);
		if ($count_sub == 0 && $count_image == 0)
			$row->delete();
		
		$rows = NULL;
		
		if ($parent_id == 0)
		{
			$rows = icategory::model()->findAll(array(
				"offset" => ($page - 1) * $this->website["beperpage"],
				"limit" => $this->website["beperpage"]
			));	
		}
		else
		{
			$rows = icategory::model()->findAll(array(
				"condition" => "parent_id = " . $parent_id,
				"offset" => ($page - 1) * $this->website["beperpage"],
				"limit" => $this->website["beperpage"]
			));	
		}
		
		if (count($rows) == 0)
		{
			if ($page > 1)
				$page --;
		}
			
		$this->redirect(array($this->controller_id.'/view',
			"parent_id"=>$parent_id, "page" => $page));
	}
	
	function actionDelete_multi($parent_id = 0, $page = 1)
	{
		//var_dump(Yii::app()->getRequest()->getPost("cib"));
		
		$id_array = Yii::app()->getRequest()->getPost("cib");
		if (!is_null($id_array))
		{
			foreach($id_array as $id=>$value)
			{
				$row = icategory::model()->findByPk($id);
				
				$count_sub = count($row->sub_category);
				$count_image = count($row->images);
				
				if ($count_sub == 0 && $count_image == 0)
					$row->delete();
			}
		}
		
		$rows = NULL;
		
		if ($parent_id == 0)
		{
			$rows = icategory::model()->findAll(array(
				"offset" => ($page - 1) * $this->website["beperpage"],
				"limit" => $this->website["beperpage"]
			));	
		}
		else
		{
			$rows = icategory::model()->findAll(array(
				"condition" => "parent_id = " . $parent_id,
				"offset" => ($page - 1) * $this->website["beperpage"],
				"limit" => $this->website["beperpage"]
			));	
		}
		
		if (count($rows) == 0)
		{
			if ($page > 1)
				$page --;
		}
		
		$this->redirect(array($this->controller_id.'/view', 
			"parent_id"=> $parent_id,"page" => $page));
	}
}